#!/bin/sh
# Start/stop/restart the AdGuard Home

bin=/usr/sbin/AdGuardHome
config=/etc/AdGuardHome.yaml
workdir=/var/lib/AdGuardHome
pidfile=/var/run/AdGuardHome.pid

start_AdGuardHome() {
  echo "Starting AdGuard Home... "
  if [ -f $pidfile ]; then
      echo "AdGuard Home is already running with PID $(cat ${pidfile})."
      exit 0
  fi
  nohup $bin --config $config --work-dir $workdir --no-check-update \
      --pidfile $pidfile 0<&- &>/dev/null &
}

stop_AdGuardHome() {
  echo "Stoppping AdGuard Home... "
  [ -f $pidfile ] && kill $(cat ${pidfile})
}

restart_AdGuardHome() {
  stop_AdGuardHome
  sleep 1
  start_AdGuardHome
}

status_AdGuardHome() {
  if [ -f $pidfile ]; then
    echo "AdGuard Home is running with PID $(cat ${pidfile})."
  else
    echo "AdGuard Home is stopped."
    exit 1
  fi
}

case "$1" in
'start')
  start_AdGuardHome
  ;;
'stop')
  stop_AdGuardHome
  ;;
'restart')
  restart_AdGuardHome
  ;;
'status')
  status_AdGuardHome
  ;;
*)
  echo "usage $0 start|stop|restart|status"
esac
